//
//  UIViewController-Extension.swift
//  zzzj
//
//  Created by Army_Ma on 2018/5/16.
//  Copyright © 2018年 Army_Ma. All rights reserved.
//

import UIKit
import CoreTelephony

extension UIViewController : SwizzlingProtocol{
    
    @objc static func awake() {
        swizzleWillAppearMethod
        swizzleWillDisAppearMethod
        swizzleDidLoadMethod
    }
    private static let swizzleWillAppearMethod: Void = {
        let originalSelector = #selector(viewWillAppear(_:))
        let swizzledSelector = #selector(new_viewWillAppear(_:))
        swizzlingForClass(UIViewController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
    }()
    
    @objc func new_viewWillAppear(_ animated: Bool) {
        new_viewWillAppear(animated)
        guard !NSStringFromClass(self.classForCoder).hasPrefix("UI") else {
            return
        }
        print("\(type(of: self)) ----- EventTrackSDK new_viewWillAppear")
        let analyticsData = AnalyticsDataView()
        analyticsData.screen_name = "\(type(of: self))"
        analyticsData.title = self.navigationItem.title ?? "ViewController"
//        analyticsData.referrer = self.parent?.navigationItem.title ?? "ViewController"
        
        SQLiteManager.shareManager().insertData(dataType: "AppViewScreen", analyticsData: analyticsData)
        
    }

    
    private static let swizzleWillDisAppearMethod: Void = {
           let originalSelector = #selector(viewWillDisappear(_:))
           let swizzledSelector = #selector(new_viewWillDisAppear(_:))
           swizzlingForClass(UIViewController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)

       }()
       
    @objc func new_viewWillDisAppear(_ animated: Bool) {
        new_viewWillDisAppear(animated)
        guard !NSStringFromClass(self.classForCoder).hasPrefix("UI") else {
            return
        }
        print("\(type(of: self)) ----- EventTrackSDK new_viewWillDisAppear")
    }
    
    private static let swizzleDidLoadMethod: Void = {
        let originalSelector = #selector(viewDidLoad)
        let swizzledSelector = #selector(new_viewDidLoad)
        swizzlingForClass(UIViewController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)

    }()
    
    @objc func new_viewDidLoad() {
        new_viewDidLoad()
        guard !NSStringFromClass(self.classForCoder).hasPrefix("UI") else {
            return
        }
        print("\(type(of: self)) ----- EventTrackSDK new_viewDidLoad")

        let viewControllers = self.navigationController?.viewControllers
           if let count = viewControllers?.count, count > 1 {
            
            let analyticsData = AnalyticsDataView()
            analyticsData.screen_name = "\(type(of: self))"
            analyticsData.title = self.navigationItem.title ?? "ViewController"
            analyticsData.referrer = self.parent?.navigationItem.title ?? "ViewController"
            SQLiteManager.shareManager().insertData(dataType: "AppViewScreen", analyticsData: analyticsData)

        }
        
    }
    
    
    
}


extension UINavigationController : SwizzlingProtocol1  {
    @objc static func awake1() {
        swizzlePushVCMethod
    }
    private static let swizzlePushVCMethod: Void = {
        let originalSelector = #selector(pushViewController(_:animated:))
        let swizzledSelector = #selector(new_pushViewController(_:animated:))
        swizzlingForClass(UINavigationController.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
    }()
    

    @objc func new_pushViewController(_ viewController: UIViewController, animated: Bool) {
        new_pushViewController(viewController, animated: animated)
        print("\(type(of: self)) ----- EventTrackSDK new_pushViewController")

    }
}
